package com.weiyi.mashangzhu.Roomer.Adapter;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.okhttp.Request;
import com.weiyi.mashangzhu.API.API;
import com.weiyi.mashangzhu.Application.MyApp;
import com.weiyi.mashangzhu.R;
import com.weiyi.mashangzhu.Roomer.Activity.HouseDetailsActivity;
import com.weiyi.mashangzhu.Roomer.Activity.OneActivityActivity;
import com.weiyi.mashangzhu.Roomer.Bean.CollectInfoBean;
import com.weiyi.mashangzhu.Roomer.Bean.MyCollectActiveBean;
import com.weiyi.mashangzhu.Utils.GlideImgManager;
import com.weiyi.mashangzhu.Utils.LoadingDialog;
import com.weiyi.mashangzhu.Utils.SPUtils;
import com.weiyi.mashangzhu.Utils.T;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by win7 on 2017/5/12.
 */

public class CollectActiveAdapter extends BaseAdapter {
    List<MyCollectActiveBean.DataBean> list;
    Context context;

    public CollectActiveAdapter(List<MyCollectActiveBean.DataBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    private void showNormalDialog(final int p) {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(context);
        normalDialog.setIcon(R.mipmap.tishi);
        normalDialog.setTitle("提示");
        normalDialog.setMessage("你确定要取消收藏此活动吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        LoadingDialog.show(context);
                        OkHttpUtils.get().addParams("token", SPUtils.get(context, "token", "") + "")
                                .addParams("id", list.get(p).getActive().getId() + "")
//                                .addParams("isCollect", "1")
                                .url(API.BaseApi_R + API.CollectActive_Api)
                                .build()
                                .execute(new StringCallback() {
                                    @Override
                                    public void onError(Request request, Exception e) {
                                        LoadingDialog.dismiss(context);
                                        T.showShort(context, "取消收藏失败");
                                    }

                                    @Override
                                    public void onResponse(String response) {
                                        Log.e("tyyyyy","re="+response);
                                        LoadingDialog.dismiss(context);
                                        try {
                                            JSONObject jsonObject = new JSONObject(response);
                                            if (jsonObject.getInt("status") == 0) {
                                                list.remove(p);
                                                notifyDataSetChanged();
                                            }
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                });
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_collect_active, null);
            viewHolder.activeText = (TextView) convertView.findViewById(R.id.active_text);
            viewHolder.activeImg = (ImageView) convertView.findViewById(R.id.active_img);
            viewHolder.activeBtn = (ImageView) convertView.findViewById(R.id.active_collect);

            convertView.setTag(viewHolder);

        }
        viewHolder = (ViewHolder) convertView.getTag();
        viewHolder.activeText.setText(list.get(position).getActive().getAcTitle() + "");
        GlideImgManager.glideLoader(context, API.BaseApi_url + list.get(position).getActive().getAcPic(), R.mipmap.errer, viewHolder.activeImg);
        viewHolder.activeBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showNormalDialog(position);
            }
        });
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, OneActivityActivity.class);
                intent.putExtra("id", list.get(position).getActive().getId() + "");
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView activeText;
        ImageView activeImg, activeBtn;
        ImageButton active_collect;
    }
}
